package com.pktechan.api.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.WxPayOrder;
import com.pktechan.api.service.WxPayOrderService;
import com.pktechan.api.utils.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 微信支付订单功能
 * @author: s.zhang
 * @create: 2021-03-0 09:10
 **/
@RestController
@RequestMapping("wxPayOrderController")
public class WxPayOrderController {
    private static final Logger logger = LoggerFactory.getLogger(WxPayOrderController.class);

    @Autowired
    private WxPayOrderService wxPayOrderService;

    /**
     * 根据id查询微信支付订单
     *
     * @param payOrderId
     * @param response
     */
    @RequestMapping(value = "getWxPayOrderById", method = RequestMethod.POST)
    public void getWxPayOrderById(@RequestParam Integer payOrderId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + payOrderId + "查询微信支付订单");
            WxPayOrder wxPayOrder = wxPayOrderService.getById(payOrderId);
            HtmlUtil.writerSuccessJson(response, wxPayOrder);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 微信支付订单列表
     *
     * @param wxPayOrder
     * @param response
     */
    @RequestMapping(value = "getWxPayOrderList", method = RequestMethod.POST)
    public void getWxPayOrderList(@RequestBody WxPayOrder wxPayOrder, HttpServletResponse response) {
        try {
            logger.info("微信支付订单列表");
            PageHelper.startPage(wxPayOrder.getPageNo(), wxPayOrder.getPageSize());
            List<WxPayOrder> list = wxPayOrderService.getList(wxPayOrder);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到微信支付订单表
     *
     * @param wxPayOrder
     * @param response
     */
    @RequestMapping(value = "saveWxPayOrder", method = RequestMethod.POST)
    public void saveWxPayOrder(@RequestBody WxPayOrder wxPayOrder, HttpServletResponse response) {
        try {
            logger.info("添加到微信支付订单表");
            wxPayOrderService.save(wxPayOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id修改微信支付订单
     *
     * @param wxPayOrder
     * @param response
     */
    @RequestMapping(value = "updateWxPayOrderById", method = RequestMethod.POST)
    public void updateWxPayOrderById(@RequestBody WxPayOrder wxPayOrder, HttpServletResponse response) {
        try {
            logger.info("根据id=" + wxPayOrder.getId() + "改微信支付订单");
            wxPayOrderService.update(wxPayOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

}
